/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.jpform.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.jpform.entity.JpTestCar;
import com.jeeplus.modules.jpform.service.JpTestCarService;

/**
 * 测试Controller
 * @author 姜盼
 * @version 2021-11-01
 */
@RestController
@RequestMapping(value = "/jpform/jpTestCar")
public class JpTestCarController extends BaseController {

	@Autowired
	private JpTestCarService jpTestCarService;

	@ModelAttribute
	public JpTestCar get(@RequestParam(required=false) String id) {
		JpTestCar entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jpTestCarService.get(id);
		}
		if (entity == null){
			entity = new JpTestCar();
		}
		return entity;
	}


	/**
	 * 测试树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(JpTestCar jpTestCar) {
		return AjaxJson.success().put("list", jpTestCarService.findList(jpTestCar));
	}

	/**
	 * 根据Id获取测试数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(JpTestCar jpTestCar) {
		return AjaxJson.success().put("jpTestCar", jpTestCar);
	}

	/**
	 * 保存测试
	 */
	@PostMapping("save")
	public AjaxJson save(JpTestCar jpTestCar, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(jpTestCar);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		jpTestCarService.save(jpTestCar);//保存
		return AjaxJson.success("保存测试成功");
	}

	/**
	 * 删除测试
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(JpTestCar jpTestCar) {
		jpTestCarService.delete(jpTestCar);
		return AjaxJson.success("删除测试成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<JpTestCar> list = jpTestCarService.findList(new JpTestCar());
		List rootTree =  jpTestCarService.formatListToTree (new JpTestCar ("0"),list, extId );
		return AjaxJson.success().put("treeData", rootTree);
	}



}